// pages/home/home.js
const request = require("../../utils/network.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.imgBase,
    horn:"",
    cartNum: null,
    products: [],
    noticeImg:[],
    isAdmin:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.readHomeData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var urlCart = '/goodsCart/getCartNum';
    request.request(urlCart, null, function (res) {
      that.setData({
        cartNum: res.data
      })
    }, function () {
    }, 'GET')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log(res)
    return {
      title: '仟禧商城',
      path: 'pages/home/home',
      imageUrl: 'https://img.alicdn.com/imgextra/i1/459645508/O1CN01fRWalF1qYi1iA9Lon_!!459645508.jpg'
    }
  },
  search: function (event) {
    var that = this;
    var url = './productSearch/productSearch';
    wx.navigateTo({
      url: url
    })
  },
  watchArea: function (event) {
    var that = this;
    var name = event.currentTarget.dataset.name;
    var url = './productArea/productArea?name=' + name;
    wx.navigateTo({
      url: url
    })
  },
  watchCart: function (event) {
    var that = this;
    var url = './productCart/productCart';
    wx.navigateTo({
      url: url
    })
  },
  watchDetail: function (event){
    var that = this;
    var id = event.currentTarget.dataset.id;
    var url = './productDetail/productDetail?id=' + id;
    wx.navigateTo({
      url: url
    })
  },
  readHomeData: function(){
    var that = this;
    var url = '/goods/getHotGoodsList';
    request.request(url, null, function (res) {
      that.setData({
        products: res.data
      })
    }, function () {

    }, 'GET')
    
    var urlNotice = '/shopNotice/getNoticeList';
    request.request(urlNotice, null, function (res) {
      if (res.data.text && res.data.text.length > 0) {
        that.setData({
          horn: res.data.text[0].content
        })
      }
      if (res.data.image && res.data.image.length > 0) {
        that.setData({
          noticeImg: res.data.image
        })
      }
    }, function () { }, 'GET')

    if (app.data.userRoleList.length >1){
      that.setData({
        isAdmin:true
      })
    }
  },
  gotoManage: function(){
    wx.redirectTo({
      url: '../index/index'
    })
  }
})